<template>
  <el-dialog :close-on-click-modal="false" :title="form.id ? '编辑' : '新增'" :visible="show" width="60%" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="班组名称" prop="name"><el-input v-model="form.name" type="text" placeholder="请输入"></el-input></el-form-item>
      <el-form-item label="考勤规则" prop="ruleId">
        <SelectByApi
          v-model="form.ruleId"
          :label.sync="form.ruleName"
          placeholder="请选择考勤规则"
          :api="ruleList"
          :params="{ pageSize: 999 }"
          labelFiled="name"
          valueFiled="id"
        ></SelectByApi>
      </el-form-item>
      <el-form-item label="考勤类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="item.value" v-for="(item, index) in checkTypes" :key="index">{{ item.label }}
            <span v-if="item.value == 2">（排班制需在 排班管理 中编辑排班设置）</span>
          </el-radio>
        </el-radio-group>
        <span></span>
        <!-- <div>（排班制需在 排班管理 中编辑排班设置）</div> -->
      </el-form-item>
      <el-form-item label="考勤日期" prop="day" v-show="form.type == 1">
        <CheckboxGroup v-model="form.day" :options="weekOpts"></CheckboxGroup>
      </el-form-item>
      <el-form-item label="考勤时间" prop="time" v-show="form.type == 1 || form.type == 0">
        <TimePickers v-model="form.time"></TimePickers>
      </el-form-item>
      <el-form-item label="考勤方式" prop="methodBody">
        <!-- <methodEdit v-model="form.methodBody"></methodEdit> -->
            <Method v-model="form.method" @change="onChangeMethod"></Method>
            <div class="lyy-card" v-if="currentMethods.includes(1)" data-title="选择考勤机设备">
                <span class="label">绑定设备</span>
                <SelectByApis v-model="form.device" :api="cardList"></SelectByApis>
            </div>
            <div class="lyy-card" v-if="currentMethods.includes(2)" data-title="小程序选择路线">
                <span class="label">小程序打卡选择路线</span>
                <RadioLine v-model="form.appRouteType"></RadioLine>
                <SelectByApis v-if="form.appRouteType == 0" v-model="form.appRoute" :placeholder="`请选择小程序路线`" :api="roadList" ></SelectByApis>
            </div>
            <div  class="lyy-card" v-if="currentMethods.includes(4)" data-title="工牌打卡选择路线">
                <span class="label">工牌打卡选择路线</span>
                <RadioLine v-model="form.cardRouteType"></RadioLine>
                <SelectByApis v-if="form.cardRouteType == 0" v-model="form.cardRoute" :placeholder="`请选择工牌打卡路线`" :api="roadList" ></SelectByApis>
            </div>
      </el-form-item>
      
        <el-form-item label="打卡规则" prop="enabled">
            <el-checkbox v-model="form.autoClock" :true-label="1" :false-label="0" @change="onChangeAutoClock">自动打卡</el-checkbox>
            <ClockRules v-if="form.autoClock == 1" v-model="form.autoClockRule"></ClockRules>
        </el-form-item>
      <el-form-item label="启用状态" prop="enabled"><el-switch v-model="form.enabled" :active-value="1" :inactive-value="0"></el-switch></el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { classAdd, classEdit } from '@/api/classGroup';
import { ruleList } from '@/api/checkRule';
import { cardList } from '@/api/card';
import { roadList } from '@/api/checkRoad';

import methodEdit from './methodEdit.vue';
import TimePickers from './timePickers.vue';
import Method from './method.vue'
import RadioLine from './radioLine.vue';
import ClockRules from './clockRules.vue';

import SelectByApis from '@/components/SelectbyApis'
export default {
  components: {
    methodEdit,
    TimePickers,
    Method,
    RadioLine,
    SelectByApis,
    ClockRules
  },
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: {}
    }
  },
  watch: {
    item: {
      handler(val) {
        this.form = val || {};
        if(val && val.method) {
            this.formatMethods(val.method)
        }
      },
      immediate: true
    },
    show: {
      handler(val) {
        val && this.resetForm('form');
      },
      immediate: true
    }
  },
  data() {
    return {
      weekOpts: [
        {
          label: '周一',
          value: '1'
        },
        {
          label: '周二',
          value: '2'
        },
        {
          label: '周三',
          value: '3'
        },
        {
          label: '周四',
          value: '4'
        },
        {
          label: '周五',
          value: '5'
        },
        {
          label: '周六',
          value: '6'
        },
        {
          label: '周日',
          value: '7'
        }
      ],
      form: {method: 1},
      rules: {
        name: [
          {
            required: true,
            message: '请输入内容',
            trigger: ['change', 'input']
          }
        ]
      },
      currentMethods: []
    };
  },
  methods: {
    ruleList,
    roadList,
    cardList,
    openRouter(path, query) {
      let routeUrl = this.$router.resolve({
        path: path || '/',
        query: {id: 22}
      });
      window.open(routeUrl.href, '_blank');
    },
    submit() {
      console.log('打印当前编辑：', this.form);
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            classEdit(this.form).then((res) => {
              // 验证成功-编辑
              this.$emit('change', this.form);
              this.close();
            });
          } else {
            classAdd(this.form).then((res) => {
              // 验证成功-新增
              this.$emit('change', this.form);
              this.close();
            });
          }
        }
      });
    },
    close() {
      this.form = {};
      this.$emit('update:show', false);
    },
    onChangeMethod(e) {
        this.currentMethods = e;
    },
    formatMethods(nv) {
        const arr = []
        if(nv >= 4) {
            arr.unshift(4)
            nv = nv - 4;
        }
        if(nv >= 2) {
            arr.unshift(2)
            nv = nv - 2;
        }
        if(nv>=1) {
            arr.unshift(1)
        }
        this.currentMethods = arr;
    },
    onChangeAutoClock(e) {
        if(this.form.autoClock == 0) {
            this.$set(this.form, 'autoClockRule', '')
        }
    }
  }
};
</script>

<style lang="scss">
.lyy-card {
    border: 1px dashed #ccc;
    padding: 10px;
    border-radius: 10px;
    margin-top: 10px;
    position: relative;
    &+.lyy-card {
        margin-top: 20px;
    }
    .label {
        position: absolute;
        top: -12px;
        background: #ffffff;
        line-height: 1.6;
        padding: 0 8px;
        font-weight: 600;
    }
}
</style>
